package com.syni.mdd.sjb.common.datatreaure.listen.userChannel;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syni.mdd.sjb.common.component.mybatis.common.entity.DxshAppUserInfo;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppMinappRecord;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppMinappRecordService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppUserInfoService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

;

/**
 * @className OrderListener
 * @description TOO
 * @Author cfx
 * @DATE 2019/8/8 15:46
 * @VERSION 1.0
 **/
@Component
@Log4j2
public class MinAppUserChannelRecordListener implements ApplicationListener<MinAppUserChannelRecordEvent> {

    @Autowired
    DxshAppUserInfoService dxshAppUserInfoService;

    @Autowired
    DxshAppMinappRecordService dxshAppMinappRecordService;

    @Override
    @Async
    public void onApplicationEvent(MinAppUserChannelRecordEvent minAppUserChannelRecordEvent) {
        Integer userId = minAppUserChannelRecordEvent.getUserId();
        String operateWord = minAppUserChannelRecordEvent.getOperateWord();
        Integer channelId = minAppUserChannelRecordEvent.getChannelId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserInfo.USER_ID,userId);
        List<DxshAppUserInfo> dxshAppUserInfos = dxshAppUserInfoService.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(dxshAppUserInfos)){
            DxshAppUserInfo dxshAppUserInfo = dxshAppUserInfos.get(0);
            DxshAppMinappRecord dxshAppMinappRecord = new DxshAppMinappRecord();
            dxshAppMinappRecord.setChannelId(channelId);
            dxshAppMinappRecord.setOperateWord(operateWord);
            dxshAppMinappRecord.setUserName(dxshAppUserInfo.getSurName() + dxshAppUserInfo.getName());
            dxshAppMinappRecord.setNewTime(LocalDateTime.now());
            dxshAppMinappRecordService.saveOrUpdate(dxshAppMinappRecord);
        }
    }
}
